package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import database.*;

public class NewServlet extends BaseServlet{
	public void page(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException {
		req.getRequestDispatcher("/page/index.jsp").forward(req, resp);
	}
	public void show(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException {
//		List<Student> stulist;
//		stulist = DP.update(Student.class,"select * from student where name='张三'" , null);
//		req.setAttribute("stulist", stulist);
		req.getRequestDispatcher("/page/stulist.jsp").forward(req, resp);
	}
//	
//	public void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		List<Student> list = new ArrayList<>();
//		System.out.println(req.getParameter("grade")+"**"+req.getParameter("c_id")+"**"+req.getParameter("m_id"));
//		String sql = Student.sqlSel(req.getParameter("grade"), req.getParameter("c_id"), req.getParameter("m_id"));
//		list = DP.update(Student.class, sql, null);
//		req.setAttribute("stulist", list);
//		req.setAttribute("sql", sql);
//		req.setAttribute("len", list.size());
//		
//		req.getRequestDispatcher("/page/stulist.jsp").forward(req, resp);
//
//}
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		DP.update(Student.class, Student.sqlUpd(
				req.getParameter("id"), req.getParameter("name"), req.getParameter("sex"), 
				req.getParameter("grade"), req.getParameter("c_id"), req.getParameter("m_id"), 
				req.getParameter("classs"), req.getParameter("company"), req.getParameter("city"),
				req.getParameter("job"), req.getParameter("money"), req.getParameter("type")), null);
	}
	public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		DP.update(Student.class, Student.sqlIns(
				req.getParameter("name"), req.getParameter("sex"), 
				req.getParameter("grade"), req.getParameter("c_id"), req.getParameter("m_id"), 
				req.getParameter("classs"), req.getParameter("company"), req.getParameter("city"),
				req.getParameter("job"), req.getParameter("money"), req.getParameter("type")), null);
	}
	public void updatepage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String sql ;
		if(req.getParameter("reset")!=null) {
			System.out.println("***reset is not null***");
			if(req.getParameter("reset").equals("reset"))
			 req.setAttribute("reset", 1);
		}
		if(req.getParameter("sql")!=null) {
			System.out.println("提交了SQL");
			sql = (String) req.getParameter("sql");
			if(sql.contains("limit")) {
				sql = sql.substring(0, sql.lastIndexOf('l')-1);
			}
		}
		else {
			System.out.println("没有提交SQL");
			sql =Student.sqlSel( req.getParameter("name"),req.getParameter("grade"), req.getParameter("c_id"), req.getParameter("m_id"));
			if(req.getParameter("grade")!=null&&!req.getParameter("grade").equals("-1")) {
				System.out.print("grade is not null");
				System.out.println(req.getParameter("grade"));
				req.setAttribute("cgrade", req.getParameter("grade"));				
			}
			if(req.getParameter("c_id")!=null&&!req.getParameter("c_id").equals("-1")) {
				System.out.print("c_id is not null");
				System.out.println(req.getParameter("c_id"));
				req.setAttribute("cc_id", req.getParameter("c_id"));				
			}
			if(req.getParameter("m_id")!=null&&!req.getParameter("m_id").equals("-1")) {
				System.out.print("m_id is not null");
				System.out.println(req.getParameter("m_id"));
				req.setAttribute("cm_id", req.getParameter("m_id"));				
			}
			System.out.println("查询之后");
		}
		//System.out.println("sql1 "+sql);
		Integer len;
		len = DP.update(Student.class, sql, null).size();
		Integer page = 1;
		if(req.getParameter("page")!=null)
		page= Integer.parseInt(req.getParameter("page"));
		Integer num = 3;
		if(req.getParameter("num")!=null)
		num=Integer.parseInt(req.getParameter("num"));
		sql =  Student.sqlpage(sql, page, num);
		//System.out.println("sql2 "+sql);
		req.setAttribute("sql",sql);
		req.setAttribute("page", page);
		req.setAttribute("len", len);
		req.getRequestDispatcher("/page/stulist.jsp").forward(req, resp);
	}
	
	public void selectM(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//组装查询sql语句
		String sql = Major.sqlSel(req.getParameter("c_id"));
		List<Major> list = DP.update(Major.class, sql, null);
		
		//json数组
//		[{"id":1, "name":"123abc"},{"id":1, "name":"123abc"},{"id":1, "name":"123abc"}]
		JSONArray arr = new JSONArray();
		for (Major major : list) {
			//创建json对象
			JSONObject obj = new JSONObject();
			//添加键值对
			obj.put("id", major.getId());
			obj.put("name", major.getName());
			//将json对象添加到json数组中
			arr.add(obj);
		}
		//创建输出流，返回专业列表的json字符串给ajax回调函数
		PrintWriter out = resp.getWriter();
		out.write(arr.toJSONString());
		out.flush();
		out.close();
		
	}
}






















